package shuiguo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import org.junit.jupiter.api.Test;

public class ShuiGuo {
	// 进货单
	private static List<JinHuoDanBean> jinHuoDanList = new ArrayList<JinHuoDanBean>();
	// 库存
	private static List<ShuiGuoBean> shuiGuoList = new ArrayList<ShuiGuoBean>();
	// 销售记录
	private static List<XiaoShouBean> xiaoShouList = new ArrayList<XiaoShouBean>();

	public static void main(String[] args) {
		while (true) {
			System.out.println("1.添加进货单");
			System.out.println("2.显示所有进货单");
			System.out.println("3.显示当前各种水果的库存数，成本单价，单价，销售数量，销售金额，进货数量，进货金额");
			System.out.println("4.加入其他水果品种");
			System.out.println("5.顾客购物");
			System.out.println("6.查询全部销售记录信息");
			System.out.println("7.按销售数量排序显示前三名");
			ShuiGuo s = new ShuiGuo();
			s.caozuo();
		}
	}

	public void caozuo() {
		Scanner sc = new Scanner(System.in);
		String xuanze = sc.nextLine();
		switch (xuanze) {
		case "1":
			tianJiaJinHuo();
			break;
		case "2":
			xianShiJinHuo();
			break;
		case "3":
			showKuCun();
			break;
		case "4":
			tianJiaOther();
			break;
		case "5":
			xiaoFei();
			break;
		case "6":
			showXiaoFei();
			break;

		case "7":
			showByXiaoShouNumber();
			break;

		default:
			break;
		}
	}

	@Test
	public void xianShiJinHuo() {
		for (int i = 0; i < jinHuoDanList.size(); i++) {
			JinHuoDanBean jinHuoDanBean = jinHuoDanList.get(i);
			System.out.println(jinHuoDanBean.toString());
		}
	}

	public void tianJiaJinHuo() {
		JinHuoDanBean jinhuo = new JinHuoDanBean();
		ShuiGuoBean kucun = new ShuiGuoBean();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入进货水果种类：");
		String shuiGuoName = sc.nextLine();
		System.out.println("请输入水果单价：");
		double xiaoShouDanJia = Double.valueOf(sc.nextLine());
		System.out.println("请输入进货数量：");
		int shuiGuoNum = Integer.parseInt(sc.nextLine());
		System.out.println("请输入进货单价：");
		double JinHuoDanJia = Double.valueOf(sc.nextLine());

		if (shuiGuoList != null && !shuiGuoList.isEmpty()) {
			for (int i = 0; i < shuiGuoList.size(); i++) {
				ShuiGuoBean shuiGuoBean = shuiGuoList.get(i);
				// 如果已经存在该水果
				if (shuiGuoBean.getShuiGuoName().equals(shuiGuoName)) {

					shuiGuoBean.setNumber(shuiGuoBean.getJinHuoNum() + shuiGuoNum);
					shuiGuoBean.setJinHuoMoney(shuiGuoNum * JinHuoDanJia);
					shuiGuoBean.setJinHuoNum(shuiGuoNum);

				}
			}
			// 添加库存
			kucun.setJinHuoNum(shuiGuoNum);
			kucun.setShuiGuoName(shuiGuoName);
			kucun.setJinHuoMoney(shuiGuoNum * JinHuoDanJia);
			kucun.setDanJia(xiaoShouDanJia);
			kucun.setNumber(kucun.getNumber() + shuiGuoNum);
			shuiGuoList.add(kucun);
		} else {
			// 添加库存
			kucun.setJinHuoNum(shuiGuoNum);
			kucun.setShuiGuoName(shuiGuoName);
			kucun.setJinHuoMoney(shuiGuoNum * JinHuoDanJia);
			kucun.setDanJia(xiaoShouDanJia);
			kucun.setNumber(kucun.getNumber() + shuiGuoNum);
			shuiGuoList.add(kucun);
		}
		// 添加进货单
		jinhuo.setShuiGuoName(shuiGuoName);
		jinhuo.setDanJia(xiaoShouDanJia);
		jinhuo.setNumber(shuiGuoNum);
		jinhuo.setJinHuoMoney(shuiGuoNum * JinHuoDanJia);
		jinhuo.setJinHuodanJia(JinHuoDanJia);
		jinHuoDanList.add(jinhuo);
	}

	public void showKuCun() {
		for (int i = 0; i < shuiGuoList.size(); i++) {
			ShuiGuoBean shuiGuoBean = shuiGuoList.get(i);
			System.out.println(shuiGuoBean.toString());
		}
	}

	/**
	 * 添加其他水果品种，在库存list中添加，如果存在就结束
	 */
	public void tianJiaOther() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入要添加的水果品种");
		String leixing = sc.nextLine();
		ShuiGuoBean shuiGuoBean = null;
		for (int i = 0; i < shuiGuoList.size(); i++) {
			shuiGuoBean = shuiGuoList.get(i);
			if (leixing.equals(shuiGuoBean.getShuiGuoName())) {
				System.out.println("该水果类型已经存在");
				return;
			}
		}
		ShuiGuoBean newShuiGuo = new ShuiGuoBean();
		// 创建一个类 ，加入shuiguolist，类只有水果名称属性
		newShuiGuo.setShuiGuoName(leixing);
		shuiGuoList.add(newShuiGuo);
		System.out.println("新品种添加成功");
	}

	/**
	 * 根据时间17:00前后，和顾客性别确定水果单价
	 */
	@Test
	public double feiYong(int sex, String shuiGuo) {
//		SimpleDateFormat fmt=new SimpleDateFormat("HH-mm-ss");
		Date time = new Date();
		Calendar cal = Calendar.getInstance();
//		String fmtTime = fmt.format(time);
		cal.setTime(time);
//		int hour = cal.HOUR_OF_DAY;
//		int min = cal.MINUTE;
		// 获取常量需要用类名调用，get方法
		int hour1 = cal.get(Calendar.HOUR_OF_DAY);
		
//		List<ShuiGuoBean> shuiGuoTwoList = Arrays.asList(new ShuiGuoBean[shuiGuoList.size()]);
//		List<ShuiGuoBean> shuiGuoTwoList = new ArrayList<ShuiGuoBean>(shuiGuoList.size());
		// 这种两个集合都改变
//		shuiGuoTwoList.addAll(shuiGuoList);
//		Collections.copy(shuiGuoTwoList, shuiGuoList);
		System.out.println("——————————————");
		
		double danjia=0;
		int status=0;
		ShuiGuoBean shui=null;
		for (int i = 0; i < shuiGuoList.size(); i++) {
			shui=shuiGuoList.get(i);
			if (shui.getShuiGuoName().equals(shuiGuo)) {
				danjia=shui.getDanJia();
				status=1;
			}
		}
		if (status==0) {
			System.out.println("没有该品种");
			return 0;
		}
		
		if (hour1 >= 17) {
			danjia=danjia*0.5;
		}
			// 没写男生情况等于舍弃这种情况
//			if (sex == 0) {
////				for (int i = 0; i < shuiGuoTwoList.size(); i++) {
////					ShuiGuoBean shuiGuoBean = shuiGuoTwoList.get(i);
////					shuiGuoBean.setDanJia(shuiGuoBean.getDanJia() * 0.5);
////				}
//			}
			if (sex == 1) {
				danjia=danjia*0.8;
//				for (int i = 0; i < shuiGuoTwoList.size(); ++i) {
////					System.out.println("女生8折");
//					ShuiGuoBean shuiGuoBean = shuiGuoTwoList.get(i);
//					shuiGuoBean.setDanJia(shuiGuoBean.getDanJia() * 0.8);
//				}
			}
			// 根据水果名称，找出对应单价
//			for (int i = 0; i < shuiGuoTwoList.size(); i++) {
//				ShuiGuoBean shuiGuoBean = shuiGuoTwoList.get(i);
//				if (shuiGuoBean.getShuiGuoName().equals(shuiGuo)) {
//					return shuiGuoBean.getDanJia();
//				}
//			}
		
		return danjia;

	}

	/**
	 * 判断输入的数据在链表中是否存在
	 * 
	 * @param <T>
	 */
//	public <ShuiGuoBean> void isExist(ArrayList<ShuiGuoBean> array,String shuru) {
//		for (int i = 0; i < array.size(); i++) {
//			ShuiGuoBean t = array.get(i);
//			
//		}
//	}

	public void xiaoFei() {
		Scanner sc = new Scanner(System.in);
		System.out.println("顾客性别");
		String xingbie = sc.nextLine();
		if (!"女".equals(xingbie)&&!"男".equals(xingbie)) {
			System.out.println("请输入男或女");
			return;
		}
		int sex = 0;
		if ("女".equals(xingbie)) {
			sex = 1;
		}
		System.out.println("买什么水果");
		String shuiGuo = sc.nextLine();
		System.out.println("购买数量");
		int number = Integer.parseInt(sc.nextLine());
		// 修改货存list中的水果数量、销售数量、销售金额，增加销售记录水果名称、顾客性别、数量、单价、销售金额
		int ex = 0;
		ShuiGuoBean shui = null;
		for (int i = 0; i < shuiGuoList.size(); i++) {
			shui = shuiGuoList.get(i);
			if (shui.getShuiGuoName().equals(shuiGuo)) {
				ex = 1;
				//找到后停止循环，否则shui会被赋值新的
				//shui是后面需要用的到的匹配正确的变量
				break;
			}
		}
		if (ex == 0) {
			System.out.println("水果品种不存在");
			return;
		} else if (ex == 1) {
			System.out.println("水果品种存在");
		}
		if (shui != null) {
			if (shui.getNumber() >= number) {
				System.out.println("库存充足");
			} else {
				System.out.println("——————————————");
				System.out.println(shui.getNumber());
				System.out.println("库存不足");
				return;
			}
		}
		
		Date cal=Calendar.getInstance().getTime();
		SimpleDateFormat dmt=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String xiaoshouTime = dmt.format(cal);
		
		ShuiGuoBean shuiGuoBean = null;
		for (int i = 0; i < shuiGuoList.size(); i++) {
			shuiGuoBean = shuiGuoList.get(i);
			XiaoShouBean xiaoshou = new XiaoShouBean();
			if (shuiGuoBean.getShuiGuoName().equals(shuiGuo)) {
				if (shuiGuoBean.getNumber() > number) {
					double money = feiYong(sex, shuiGuo);
					shuiGuoBean.setNumber(shuiGuoBean.getNumber() - number);
					shuiGuoBean.setXiaoShouNum(shuiGuoBean.getXiaoShouNum() + number);
					shuiGuoBean.setXiaoShouMoney(shuiGuoBean.getXiaoShouMoney() + number * money);
					
					xiaoshou.setXiaoShouTime(xiaoshouTime);
					xiaoshou.setDanJia(money);
					xiaoshou.setNumber(number);
					xiaoshou.setSex(sex);
					xiaoshou.setShuiGuoName(shuiGuo);
					xiaoshou.setXiaoShouMoney(shuiGuoBean.getXiaoShouMoney() + number * money);
					xiaoShouList.add(xiaoshou);
					break;
				} else {
					System.out.println("库存不足");
					break;
				}
			}

		}

	}

	public void showXiaoFei() {
		System.out.println("全部消费记录：");
		for (int i = 0; i < xiaoShouList.size(); i++) {
			XiaoShouBean xiaoShouBean = xiaoShouList.get(i);

			System.out.println(xiaoShouBean.toString());
		}
	}

	public void showByXiaoShouNumber() {

		java.util.Collections.sort(xiaoShouList, new Comparator<XiaoShouBean>() {
			@Override
			public int compare(XiaoShouBean o1, XiaoShouBean o2) {
				return o2.getNumber() - o1.getNumber();
			}
		});
		System.out.println("按消费数量排名前三消费记录：");
		for (int i = 0; i < 3; i++) {

			XiaoShouBean xiaoShouBean = xiaoShouList.get(i);
			System.out.println(xiaoShouBean.toString());

		}

	}
}
